//
//  ZMTool.m
//  ZMClient
//
//  Created by 我的苹果 on 16/1/27.
//  Copyright © 2016年 alan. All rights reserved.
//

#import "ZMTool.h"
#import "Header.h"
@implementation ZMTool
//时间戳转换
+ (NSString *)getDateTimeString:(NSString *)timeStampStr DateFormat:(NSString *)format
{
    if(!timeStampStr){
        return nil;
    }
    NSString * timeStampString = [NSString stringWithFormat:@"%@",timeStampStr];
    NSTimeInterval _interval=[timeStampString doubleValue] / 1000.0;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    
    NSTimeZone *defaultTimeZone = [NSTimeZone defaultTimeZone];
    NSTimeZone *tzGMT = [NSTimeZone timeZoneWithName:@"GMT+0800"];
    [NSTimeZone setDefaultTimeZone:tzGMT];
    [objDateformat setTimeZone:defaultTimeZone];
  
    [objDateformat setDateFormat:format];
    NSString *inputString = [objDateformat stringFromDate: date];
    return inputString;
}
//验证手机号
+(BOOL)validateTel:(NSString *)tel
{
    NSString *telStr = @"^(1[3-8])\\d{9}$";
    NSPredicate *telTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", telStr];
    return [telTest evaluateWithObject:tel];
}
+ (CGFloat)getLabelHeight:(NSString *)string font:(UIFont *)font width:(CGFloat)width
{
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName,nil];
    CGSize textSize = CGSizeMake(width, 0.0f);
    textSize = [string boundingRectWithSize:textSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading  attributes:dict context:nil].size;
    return textSize.height;
}

+(void)showAlertWithDifferentVersioninWhichViewController:(UIViewController *)vc  withTitle:(NSString *)title andMessage:(NSString *)message andSureTitle:(NSString *)sureTitle andCancleTitle:(NSString *)cancleTitle isDelegate:(BOOL)isDelegate andSureBlock:(void(^)(UIAlertAction *  action))sureBlock andCancleBlock:(void(^)(UIAlertAction * action))cancleBlock
{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:isDelegate?vc:nil cancelButtonTitle:cancleTitle otherButtonTitles:sureTitle, nil];
        [alert show];
    }else
    {
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
        
        if (cancleTitle)
        {
            UIAlertAction *cancle=[UIAlertAction actionWithTitle:cancleTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                if (isDelegate&&cancleBlock) {
                    cancleBlock(action);
                }
            }];
            [alert addAction:cancle];
        }
        
        if (sureTitle)
        {
            UIAlertAction *sureAction=[UIAlertAction actionWithTitle:sureTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (isDelegate&&sureBlock) {
                    sureBlock(action);
                }
            }];
            [alert addAction:sureAction];
        }
        
        [vc presentViewController:alert animated:YES completion:nil];
    }
}
+(void)showActionSheetWithDifferentVersioninWhichViewController:(id)vc  withTitle:(NSString *)title andMessage:(NSString *)message andSureTitleOne:(NSString *)sureTitleOne andSureTitleTwo:(NSString *)sureTitleTwo andSureTitleThree:(NSString *)SureTitleThree andCancleTitle:(NSString *)cancleTitle isDelegate:(BOOL)isDelegate andSureBlockOne:(void(^)(UIAlertAction *  action))sureBlockOne andSureBlockTwo:(void(^)(UIAlertAction *  action))sureBlockTwo andSureBlockThree:(void(^)(UIAlertAction *  action))SureBlockThree andCancleBlock:(void(^)(UIAlertAction * action))cancleBlock
{
    UIViewController * showVc = (UIViewController *)vc;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8.0)
    {
        
        UIActionSheet *menu=[[UIActionSheet alloc] initWithTitle:title delegate:isDelegate?vc:nil cancelButtonTitle:cancleTitle destructiveButtonTitle:nil otherButtonTitles:sureTitleOne,sureTitleTwo, nil];
        menu.actionSheetStyle=UIActionSheetStyleBlackTranslucent;
        [menu showInView:showVc.view];
    }else
    {
        UIAlertController *alert=[UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleActionSheet];
        UIButton * btn = (UIButton *)[showVc.view viewWithTag:555];
        if (btn)
        {
            alert.popoverPresentationController.sourceView = btn;
            alert.popoverPresentationController.sourceRect = btn.bounds;
        }
        
        if (cancleTitle)
        {
            UIAlertAction *cancle=[UIAlertAction actionWithTitle:cancleTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                if (isDelegate&&cancleBlock) {
                    cancleBlock(action);
                }
            }];
            [alert addAction:cancle];
        }
        
        if (sureTitleOne)
        {
            UIAlertAction *sureAction=[UIAlertAction actionWithTitle:sureTitleOne style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (isDelegate&&sureBlockOne) {
                    sureBlockOne(action);
                }
            }];
            [alert addAction:sureAction];
        }
        if (sureTitleTwo)
        {
            UIAlertAction *sureAction=[UIAlertAction actionWithTitle:sureTitleTwo style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (isDelegate&&sureBlockTwo) {
                    sureBlockTwo(action);
                }
            }];
            [alert addAction:sureAction];
        }
        if (SureBlockThree) {
            UIAlertAction *sureAction=[UIAlertAction actionWithTitle:SureTitleThree style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                if (isDelegate&&SureBlockThree) {
                    SureBlockThree(action);
                }
            }];
            [alert addAction:sureAction];
        }
        [vc presentViewController:alert animated:YES completion:nil];
    }
}
+(BOOL)isEmptyStrWithString:(NSString *)str
{
    if (self == nil || self == NULL) {
        return  YES;
    }
    if ([str isKindOfClass:[NSNull class]]) {
        return  YES;
    }
    
    if ([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        
        return  YES;
    }
    BOOL isStringtoSpace=YES;
    NSString *strSpace =@" ";
    for(int i =0;i<[str  length];i++)    {
        NSString *string = [str substringWithRange:NSMakeRange(i, 1)];//抽取子字符
        if(![string isEqualToString:strSpace]){//判断是否为空格
            isStringtoSpace=NO; //如果是则改变 状态
            break;//结束循环
        }
    }
    return isStringtoSpace;
}
@end
